﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domino;

namespace Streambolics.Notes
{
    /// <summary>
    ///     A View in a Notes Database.
    /// </summary>
    /// <remarks><para>
    ///     The View object encapsulates the low-level NotesView
    ///     COM object.
    /// </para></remarks>
    public class View : IHasDocuments
    {
        #region Private Variables

        private NotesView _View;
        private Database _Database;
        private string _Name;

        #endregion

        #region Constructors

        internal View (NotesView aView)
        {
            _View = aView;
            _Name = aView.Name;
        }

        internal void Attach (Database aDatabase)
        {
            _Database = aDatabase;
        }

        #endregion

        public override string ToString ()
        {
            return _Name;
        }

        public string Name
        {
            get
            {
                return _Name; 
            }
        }

        public bool IsSpecial
        {
            get
            {
                return _Name.StartsWith("($");
            }
        }

        public IEnumerable<string> Hierarchy
        {
            get
            {
                return _Name.Split ('\\');
            }
        }

        #region Documents

        /// <summary>
        ///     All documents in the view
        /// </summary>

        public IEnumerable<Document> Documents
        {
            get
            {
                NotesViewEntryCollection nvc = _View.AllEntries;
                for (int i = 1; i <= nvc.Count; i++)
                {
                    yield return CachedDocument (nvc.GetNthEntry (i).Document);
                }
            }
        }

        private Document CachedDocument (NotesDocument nd)
        {
            return _Database.CachedDocument (nd);
        }

        #endregion
    }
}
